﻿using System.Web.Mvc;
using Inovout.Membership.Security;
using Inovout.Membership.Security.OAuth.AuthorizationServer;
using Inovout.Security;
using Inovout.Web.Mvc.Security;

namespace Inovout.Membership.Account.WebPage.Controllers
{
    [AllowAnonymous]
    public class AccountController : OAuthAccountController
    {
        private DynamicPasswordCredentialProvider dynamicPasswordCredentialProvider;
        private CredentialValidator credentialValidator;

        public AccountController(DynamicPasswordCredentialProvider dynamicPasswordCredentialProvider, CredentialValidator credentialValidator)
            : base(credentialValidator)
        {
            //由于Autfofac对泛型支持不太好，不能通过构造器注入触发Actived事件，故无法配置Repository
            this.dynamicPasswordCredentialProvider = dynamicPasswordCredentialProvider;
            this.credentialValidator = credentialValidator;
        }

        /// <summary>
        /// 默认为Forms认证
        /// </summary>
        /// <returns></returns>
        public ActionResult Forms(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }
        [HttpPost]
        public ActionResult Forms(IFormsCredential formsCredentiaInfo, string returnUrl)
        {
            return SignIn(formsCredentiaInfo, returnUrl);
        }

        public ActionResult DynamicPassword(string returnUrl)
        {
            ViewBag.Status = "加入我们";
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        public ActionResult DynamicPassword(IDynamicPasswordCredential dynamicPasswordCredentiaInfo, string returnUrl)
        {
            return SignIn(dynamicPasswordCredentiaInfo, returnUrl);
        }
        public JsonResult GetDynamicPassword(string phone)
        {
            DynamicPassword dynamicPassword = dynamicPasswordCredentialProvider.GetDynamicPassword(phone);
            // credentialValidator.SendDynamicPassword(dynamicPassword);
            return Json(dynamicPassword, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Mail(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        public ActionResult Mail(IMailAuthenticationCredential mailCredentialInfo, string returnUrl)
        {

            return SignIn(mailCredentialInfo, returnUrl);
        }


    }
}